package com.team1.system.libraryrating.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.team1.common.annotation.Log;
import com.team1.common.core.controller.BaseController;
import com.team1.common.core.domain.AjaxResult;
import com.team1.common.enums.BusinessType;
import com.team1.system.libraryrating.domain.LibraryRating;
import com.team1.system.libraryrating.service.ILibraryRatingService;
import com.team1.common.utils.poi.ExcelUtil;
import com.team1.common.core.page.TableDataInfo;

/**
 * 评价管理Controller
 *
 * @author hyy
 * @date 2023-04-09
 */
@RestController
@RequestMapping("/librarysystem/libraryrating")
public class LibraryRatingController extends BaseController
{
    @Autowired
    private ILibraryRatingService libraryRatingService;

    /**
     * 查询评价管理列表
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryrating:list')")
    @GetMapping("/list")
    public TableDataInfo list(LibraryRating libraryRating)
    {
        startPage();
        List<LibraryRating> list = libraryRatingService.selectLibraryRatingList(libraryRating);
        return getDataTable(list);
    }

    /**
     * 导出评价管理列表
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryrating:export')")
    @Log(title = "评价管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LibraryRating libraryRating)
    {
        List<LibraryRating> list = libraryRatingService.selectLibraryRatingList(libraryRating);
        ExcelUtil<LibraryRating> util = new ExcelUtil<LibraryRating>(LibraryRating.class);
        util.exportExcel(response, list, "评价管理数据");
    }

    /**
     * 获取评价管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryrating:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(libraryRatingService.selectLibraryRatingByUserId(userId));
    }

    /**
     * 新增评价管理
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryrating:add')")
    @Log(title = "评价管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LibraryRating libraryRating)
    {
        libraryRating.setUserId(getUserId());
        LibraryRating temp=libraryRatingService.selectLibraryRatingByUserIdBookId(libraryRating);
        if (temp.getBookId().equals(libraryRating.getBookId())){
            return error("已对该图书评价");
        }
        return toAjax(libraryRatingService.insertLibraryRating(libraryRating));
    }

    /**
     * 修改评价管理
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryrating:edit')")
    @Log(title = "评价管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LibraryRating libraryRating)
    {
        return toAjax(libraryRatingService.updateLibraryRating(libraryRating));
    }

    /**
     * 删除评价管理
     */
    @PreAuthorize("@ss.hasPermi('librarysystem:libraryrating:remove')")
    @Log(title = "评价管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(libraryRatingService.deleteLibraryRatingByUserIds(userIds));
    }
}
